package com.xbongbong.parent.field.impl;

import com.alibaba.fastjson.JSONObject;
import com.xbongbong.parent.field.FieldAnalyseService;
import com.xbongbong.paas.domain.entity.ext.PaasFormDataEntityExt;
import com.xbongbong.paas.enums.FieldTypeEnum;
import com.xbongbong.paas.field.FieldAttrEntity;
import com.xbongbong.paas.field.FilterConditionsPoJo;
import com.xbongbong.paas.toolbox.exception.XbbException;
import com.xbongbong.pro.businessrule.pojo.BusinessRuleInfoPojo;
import com.xbongbong.saas.enums.UserTeamEnum;
import com.xbongbong.saas.service.toolbox.help.UserTeamHelp;
import com.xbongbong.sys.domain.entity.UserEntity;
import com.xbongbong.saas.util.UserFieldUtil;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * @author 吴峰
 * @date 2021/07/07 14:25
 */
@Service("coUserIdFieldAnalyseServiceImpl")
public class CoUserIdFieldAnalyseServiceImpl implements FieldAnalyseService {


    @Resource
    private UserTeamHelp userTeamHelp;

    @Override
    public List<Integer> fieldType() {
        return Arrays.asList(FieldTypeEnum.COUSERID.getType());
    }

    @Override
    public Object analysisData(Long formId, Integer businessType, Integer saasMark, PaasFormDataEntityExt paasFormDataEntityExt, JSONObject data, FieldAttrEntity fieldAttrEntity) throws XbbException {
        if (UserTeamEnum.hasTeam(businessType)) {
            List<Long> idList = new ArrayList<>();
            idList.add(paasFormDataEntityExt.getId());
            List<UserEntity> userList = userTeamHelp.getUserTeamList(idList, paasFormDataEntityExt.getCorpid(), businessType, false, 0, true);
            return UserFieldUtil.getNameByUserList(userList);
        } else {
            // TODO 自定义协同人
        }
        return null;
    }

    @Override
    public Object handlerPreConditionValue(FieldAttrEntity thisField, BusinessRuleInfoPojo businessRuleInfoPojo, FilterConditionsPoJo filterCondition) throws XbbException {
        return UserFieldUtil.getUserIdAndDeptIdList(filterCondition.getValue(), filterCondition.getSymbol());
    }
}
